/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.Departamento;
import ve.usb.coord.colision.hibernate.HibernateUtil;
import ve.usb.coord.colision.hibernate.Oferta;

/**
 *
 * @author Fernando
 */
public class AccionesOfertaInfo extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        
        try {
            String crit = (String)request.getSession().getAttribute("OfertaBuscar");
            if(Integer.parseInt(request.getSession().getAttribute("tipoUsuario").toString()) != 0){
                if (crit==null) crit = " where planDeEstudios.coordinacion = "+request.getParameter("idCoordinacion");
                else request.getSession().removeAttribute("OfertaBuscar");
            }else{
                if (crit==null) crit = "";
                else request.getSession().removeAttribute("OfertaBuscar");
            }
            List<Oferta> list = (List<Oferta>)s.createQuery("from Oferta"+crit).list();
            request.setAttribute("L_Oferta", list);
            for (Oferta oferta:list) {
                if (oferta.getPeriodo()!=null) oferta.getPeriodo().getNombre();
            }
            for (Oferta oferta:list) {
                if (oferta.getPlanDeEstudios()!=null) oferta.getPlanDeEstudios().getCoordinacion();
            }
            for (Oferta oferta:list) {
                if (oferta.getRequisito()!=null) oferta.getRequisito().getAsignatura().getCodigo();
            }
            List<Departamento> listD = (List<Departamento>)s.createQuery("from Departamento").list();
            request.setAttribute("L_Departamento", listD);

            tr.commit();

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return mapping.findForward(SUCCESS);
        
    }
}